import axios from 'axios';
import METHOD from './methods';
import Loading from '../components/loading/index'
import Vue from 'vue'
const baseURL="https://api.it120.cc/small4";

//创建axios配置实例
var axiosInstance=axios.create({
  baseURL:baseURL,
  // timeout:1000,
})

var loading=null;
var count=0;
//请求发送前 弹出loading组件
axiosInstance.interceptors.request.use(function(config){
  //在请求发送前 弹出loading
  loading=Loading.serve();
  // console.log(count++)
  Vue.nextTick(()=>{
    loading.addCount()
    // console.log(loading.count)
  })
  
  return config;
},function(error){
  return Promise.reject(error);
})

//请求结束后 收起loading组件
axiosInstance.interceptors.response.use(function(response){
    Vue.nextTick(()=>{
      loading.minusCount()
    })
  return response;
},function(error){
  Vue.nextTick(()=>{
    loading.minusCount()
  })
  return Promise.reject(error);
})

export function request(methods,url,params){
  switch(methods){
    case METHOD.GET:
      return GET(url,params);
    case METHOD.POST:
      return POST(url,params);  
  }
}

function GET(url,params){
  return axiosInstance.get(url,params)
}

function POST(url,params){
  return axiosInstance.post(url,params)
}